package com.baselibrary.libs.View.TestView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import com.baselibrary.libs.Utils.log.LogUtils;

/**
 * Created by Administrator 于萌萌
 * 创建日期: 16:10 . 2017年03月03日
 * 描述:圆
 * <p>
 * <p>
 * 备注:
 */

public class MyView1 extends View {

    private int mHeightPixels;
    private int mWidthPixels;
    private RectF mRectF;
    private Paint mPaint;

    /**
     * 最大进度
     */
    private int max = 100;

    /**
     * 当前进度
     */
    private int progress = 0;

    public MyView1(Context context) {
        super(context);
        initView(context);
    }



    public MyView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MyView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        DisplayMetrics dm2 = getResources().getDisplayMetrics();
        mHeightPixels = dm2.heightPixels;
        mWidthPixels = dm2.widthPixels;
        LogUtils.i("屏幕宽高是"+"heightPixels"+ mHeightPixels +"\t"+"widthPixels"+ mWidthPixels);
        mRectF = new RectF();
        mPaint = new Paint();

    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int desiredWidth ;
        int desiredHeight ;

            desiredWidth =500;
            desiredHeight = 500;

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;

        //Measure Width
        if (widthMode == MeasureSpec.EXACTLY) {
            //Must be this size
            width = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            //Can't be bigger than...
            width = Math.min(desiredWidth, widthSize);
        } else {
            //Be whatever you want
            width = desiredWidth;
        }

        //Measure Height
        if (heightMode == MeasureSpec.EXACTLY) {
            //Must be this size
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            //Can't be bigger than...
            height = Math.min(desiredHeight, heightSize);
        } else {
            //Be whatever you want
            height = desiredHeight;
        }

        //MUST CALL THIS
        setMeasuredDimension(width, height);

//        center = getWidth()/2; //该方法必须在onDraw或者onMeasure中调用，否则不起作用d
//
//        //圆环的半径 ，此处必须是progressWidth与circleWidth中较大的一个
//        //radius = (int) (center - progressWidth/2);
//        if(progressWidth>circleWidth)
//            radius=(int)(center-progressWidth/2);
//        else
//        {radius=(int)(center-circleWidth/2);}
//
//
//        sweepGradient = new SweepGradient(0, 0, colors, null);
//        hideRect=new RectF(center - radius, center - radius, center
//                + radius, center + radius);

    }
    public static int getDefaultSize(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                result = size;
                break;
            case MeasureSpec.AT_MOST:
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 画布 canvas
        // 画笔 paint
        //抗锯齿
//        paint.setAntiAlias(true);
//        //设置画笔颜色
//        paint.setColor(Color.RED);
//        //笔的样式 镂空 或者全覆盖
//        paint.setStyle(Paint.Style.STROKE);
//        //设置画笔宽度
//        paint.setStrokeWidth(5);

        // 设置画笔相关属性
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
        canvas.drawColor(Color.TRANSPARENT);
        mPaint.setStrokeWidth(8);
        mPaint.setStyle(Paint.Style.STROKE);
        /**
         * 这是一个居中的圆
         */
        float x = (getWidth() - getHeight() / 2) / 2;
        float y = getHeight() / 4;

        RectF oval = new RectF( x, y,
                getWidth() - x, getHeight() - y);

        // 位置
        mRectF.left = mWidthPixels / 2; // 左上角x
        mRectF.top = mHeightPixels / 2; // 左上角y
        mRectF.right = mWidthPixels/ 2; // 左下角x
        mRectF.bottom = mHeightPixels/ 2; // 右下角y
        int radius = (int) (x/2); //圆环的半径
       // canvas.drawCircle(x,y,radius,mPaint);

        LogUtils.i("现在的尺寸"+oval);

        canvas.drawArc(oval, -90, 360, false, mPaint);  //根据进度画圆弧
        mPaint.setStrokeWidth(12);
        mPaint.setColor(Color.rgb(255, 50, 50));
//        RectF oval = new RectF(mRectF.left - radius, mRectF.left - radius,  mRectF.top
//                + radius,  mRectF.top + radius);  //用于定义的圆弧的形状和大小的界限

        canvas.drawArc(oval, -90, 360 * progress / max, false, mPaint);  //根据进度画圆弧

      //  canvas.drawPoint(getWidth()/2,getHeight()/2,mPaint);
       // canvas.drawPoint(oval.right,oval.bottom,mPaint);
        mPaint.setTextSize(30);
        mPaint.setStrokeWidth(3);
        mPaint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(progress+"%",getWidth()/2,getHeight()/2+5,mPaint);

        //画单线
       // canvas.drawLine(100,100,200,200,paint);
        //画点
       // canvas.drawPoint(100,100,paint);


      //  canvas.drawCircle(100,100,50,paint);
        //设置画布背景颜色
     //   canvas.drawRGB(255, 255,255);
        //使用画矩形
//        RectF rect = new RectF(120, 10, 210, 100);
//        canvas.drawRect(rect, paint);

//        Path path = new Path();
//
//        path.moveTo(100,100);
//
//        path.lineTo(300,300);
//
//        path.lineTo(300,400);
//
//        path.lineTo(500,100);
//
 //       path.close();
//        //画矩形
//        RectF rect = new RectF(100, 100, 200, 200);
//
//        Path path = new Path();
//        //逆时针 画矩形
//        path.addRect(rect, Path.Direction.CCW);

     //   canvas.drawPath(path,paint);


    }

    public MyView1 setProgress(int progress){
        this.progress = progress;
        invalidate();
        return this;
    }

}
